﻿using Flurl.Http;
using Newtonsoft.Json.Linq;
using Pay.Core;
using Pay.DTO.mpay;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Pay.Service {
    public class MpaySign {
        public MpaySign() {

        }
#if DEBUG
        private string pay_url = "https://fjymxx.com/skl/router?_method=createUnionPayOrder";
#else
        private string pay_url = "https://mhskl.com/sklos/order/createUnionPayOrder";
#endif
        private string key = "C929A06CE0D0156D98289A86A581FFE8";
        public mpay_error_model  PayGateway(mpay_model model)  {
            var log = IocHelper.Get<ILogHelper>("info");
            try {
                model.sign = Sign(model);
                var result = pay_url.PostJsonAsync(model).ReceiveJson<mpay_error_model>().Result;
                log.Error(getErrorMsg(result.ret));
                return result;
            } catch (FlurlHttpException ex) {
                log.Error(ex.Message);
                return null;
            }
        }
        public string Sign(mpay_model model) {
            string json = model.ToJson();
            var collection = json.ToObject<SortedDictionary<string, object>>();
            collection.Remove("sign");
            string result = string.Empty;
            foreach (var kv in collection) {
                if (kv.Value != null && !string.IsNullOrEmpty(kv.Value.ToString()))
                    result += "&" + kv.Key + "=" + kv.Value;
            }
            result = result.Trim('&') + "&KEY=" + key;
            var log = IocHelper.Get<ILogHelper>("info");
            log.Error(result);

            result = SecureHelper.MD5(result).ToUpper();
            return result;
        }
        public bool VerifySign(mpay_notice model) {
            string json = model.ToJson();
            var collection = json.ToObject<SortedDictionary<string, object>>();
            var sign = collection["sign"].ToString();
            collection.Remove("sign");
            string result = string.Empty;
            foreach (var kv in collection) {
                if (kv.Value != null && !string.IsNullOrEmpty(kv.Value.ToString()))
                    result += "&" + kv.Key + "=" + kv.Value;
            }
            result = result.Trim('&') + "&KEY=" + key;
            Console.WriteLine(result);
            result = SecureHelper.MD5(result).ToUpper();
            Console.WriteLine(result);
            return result == sign;
        }
        public string getErrorMsg(string ret) {
            string str = string.Empty;
            switch (ret) {
                case "1": str = "成功"; break;
                case "-43": str = "时间戳超出时效"; break;
                case "-44": str = "非法请求数据"; break;
                case "-46": str = "signType 错误"; break;
                case "-47": str = "不在交易时间范围"; break;
                case "-48": str = "交易金额不能大于 19800 元。"; break;
                case "-60": str = "回调地址过长"; break;
                case "-90": str = "系统异常"; break;
                case "-99": str = "参数错误"; break;
            }
            return str;
        }
    }
}
